#include "logopainter.h"

#include <QThread>
#include <QTimer>
#include <QDebug>


LogoPainter::LogoPainter(QWidget *parent) :
    QWidget(parent)
{
    _pen.setColor(Qt::black);
    _pen.setWidth(3);
    _picTurtle.load(":/img/turtle.png");

}

void LogoPainter::paraFrente(int steps)
{
    _walk(steps, 1);
}

void LogoPainter::paraTras(int steps)
{
    _walk(steps, -1);
}

void LogoPainter::girarDireita(int angulo)
{
    _rotate(angulo);
}

void LogoPainter::girarEsquerda(int angulo)
{
    _rotate(-angulo);
}

void LogoPainter::pular(int steps)
{
    Jump *jump = new Jump(0, steps);
    _listCmds.append(jump);
}

void LogoPainter::forca(int forca)
{
    _pen.setWidth(forca);
}

void LogoPainter::limpar()
{
    for(auto cmd : _listCmds) {
        delete cmd;
    }
    _listCmds.clear();
}

void LogoPainter::mudarCor(QColor cor)
{
    //_pen.setColor( cor );
    Color *color = new Color(cor);
    _listCmds.append(color);
}

void LogoPainter::desenhaTudo(bool stepByStep)
{
    qDebug() << "desenhaTudo" << QThread::currentThreadId();
    if( stepByStep ) {
        _curStep = 0;
        QTimer::singleShot(500, this, SLOT(_nextStep()));
    } else {
        _curStep = -1;
        this->repaint();
    }
}

void LogoPainter::_walk(int steps, int direction)
{
    Line *line;
    if( direction == 1 )
        line = new Line(0, 0, 0, steps);
    else
        line = new Line(0, 0, 0, -steps);
    _listCmds.append(line);
}

void LogoPainter::_rotate(int angle)
{
    Rotate *rotate = new Rotate(angle);
    _listCmds.append(rotate);
}

void LogoPainter::_runCommand(QPainter *painter, Command *command)
{
    if( command->commandType == Command::LINE ) {
        Line *line = (Line*)command;
        painter->drawLine(line->x1, line->y1, line->x2, line->y2);
        painter->translate(line->x2, line->y2);
    } else if( command->commandType == Command::ROTATE ) {
        Rotate *rot = (Rotate*)command;
        painter->rotate( rot->angle );
    } else if( command->commandType == Command::JUMP ) {
        Jump *jump = (Jump*)command;
        painter->translate(jump->x2, jump->y2);
    } else if( command->commandType == Command::COLOR ) {
        Color *color = (Color*)command;
        _pen.setColor( color->cor );
        painter->setPen(_pen);
    }

}

void LogoPainter::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);


    //Inicializando ----------
    qreal dx, dy;
    dx = this->width() / 2.0;
    dy = this->height() - 50.0;

    painter.translate(dx, dy);
    painter.rotate(180);

    painter.setPen(_pen);
    painter.setBrush(_brush);
    //------------------------

    int counter = 0;
    //for(auto cmd : _listCmds) {
    int steps = _listCmds.size();
    int ini = 0;
    for(int i=ini; i < steps; i++) {

        auto cmd = _listCmds[i];
        _runCommand(&painter, cmd);

        if( _curStep == -1 ) //animation disabled
            continue;

        if( counter > _curStep ) //Estamos no passo atual já
            break;

        counter++;

    }

    painter.rotate(180);
    dx = ((qreal)_picTurtle.width()) / 2.0;
    dy = ((qreal)_picTurtle.height());
    painter.translate(-dx, -dy);
    painter.drawPixmap(0, 0, _picTurtle);

    qDebug() << "Paint" << QThread::currentThreadId();

}

void LogoPainter::_nextStep()
{
    this->repaint();

    qDebug() << "_nextStep" << QThread::currentThreadId();
    _curStep++;
    if( _curStep < _listCmds.size() ) {
        QTimer::singleShot(500, this, SLOT(_nextStep()));
    }
}
